package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult list(CategoryPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page page = (Page) categoryMapper.list(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category, "id");
        category.setStatus(0);
/*        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        log.info("category:{}", category);
        categoryMapper.insert(category);
    }

    @Override
    public void update(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
/*        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.update(category);
    }

    @Override
    public List<Category> listBytype(Integer type) {
        List<Category> list = categoryMapper.listBytype(type);
        return list;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Category category = Category.builder()
                .id(id)
                .status(status)
/*                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())*/
                .build();

        categoryMapper.update(category);
    }

    @Override
    public void deleteById(Long id) {
       Integer dishCount =  dishMapper.countCategory(id);
       Integer setmealCount = setmealMapper.countCategory(id);
       if(dishCount>0){
           throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
       }
       if(setmealCount>0){
           throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);

       }
       categoryMapper.deleteById(id);
    }
}
